package com.monkey.tensquare.base.controller;

import com.monkey.tensquare.base.entity.Label;
import com.monkey.tensquare.base.mapper.LabelMapper;

import com.monkey.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/label")
public class LabelController {
    @Autowired
    private LabelService labelService;

    /**
     * 1 查询全部标签列表
     *
     * @return 结果集
     */
    @GetMapping
    public Result findAll() {
        try {
            List<Label> labelList = labelService.findAll();
            return new Result(StateCode.OK, true, "查询全部标签列表成功！", labelList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR, false, "查询全部标签列表失败！", null);
        }

    }

    /**
     * 2 添加标签
     *
     * @param label 标签
     * @return 添加结果
     */
    @PostMapping
    public Result add(@RequestBody Label label) {
        try {
            labelService.add(label);
            return new Result(StateCode.OK, true, "添加标签成功！", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR, false, "添加标签失败！", null);
        }
    }

    /**
     * 3 查询推荐标签
     *
     * @return
     */
    @GetMapping("/toplist")
    public Result topList() {
        try {
            List<Label> topList = labelService.topList();
            return new Result(StateCode.OK, true, "查询推荐标签列表成功！", topList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR, false, "查询推荐标签列表失败！", null);
        }
    }

    /**
     * 4 根据ID查询标签
     *
     * @param labelId
     * @return
     */
    @GetMapping("/{labelId}")
    public Result findById(@PathVariable String labelId) {
        try {
            Label label = labelService.findById(labelId);
            return new Result(StateCode.OK, true, "查询标签成功！", label);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR, false, "查询标签失败！", null);
        }
    }

    /**
     * 5 修改标签
     *
     * @param labelId
     * @param label
     * @return
     */
    @PutMapping("/{labelId}")
    public Result update(@PathVariable String labelId, @RequestBody Label label) {
        try {
            label.setId(labelId);
            labelService.update(label);
            return new Result(StateCode.OK, true, "修改标签成功！", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR, false, "修改标签失败！", null);
        }
    }

    /**
     * 6 删除标签
     *
     * @param labelId
     * @return
     */
    @DeleteMapping("/{labelId}")
    public Result delete(@PathVariable String labelId) {
        try {
            labelService.delete(labelId);
            return new Result(StateCode.OK, true, "删除标签成功！", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR, false, "删除标签失败！", null);
        }
    }

    /**
     * 7 搜索标签
     *
     * @param label
     * @return
     */
    @PostMapping("/search")
    public Result search(@RequestBody Label label) {
        try {
            List<Label> labelList = labelService.search(label);
            return new Result(StateCode.OK, true, "搜索标签成功！", labelList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR, false, "搜索标签失败！", null);
        }
    }

    /**
     * 查询有效标签
     *
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        try {
            List<Label> labelList = labelService.list();
            return new Result(StateCode.OK, true, "查询有效标签成功！", labelList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR, false, "查询有效标签失败！", null);
        }
    }

    /**
     * 条件查询带分页
     *
     * @param label
     * @return
     */
    @PostMapping("/search/{page}/{size}")
    public Result search(@RequestBody Label label, @PathVariable Integer page, @PathVariable Integer size) {
        try {
            PageResult<Label> labelPageResult = labelService.search(label, page, size);
            return new Result(StateCode.OK, true, "分页搜索标签成功！", labelPageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR, false, "分页搜索标签失败！", null);
        }
    }

}
